package scau.zxck.service;

import scau.zxck.constant.CODEDEMO;
import scau.zxck.constant.FLAG;
import scau.zxck.constant.PATH;
import scau.zxck.entity.ColumnInfo;
import scau.zxck.entity.TableInfo;
import scau.zxck.utils.FileUtil;
import scau.zxck.utils.NameUtil;
import scau.zxck.utils.PathUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lsp on 27/11/2016.
 */
public class CreatorService {
    private String outDir;
    private TableInfo tableInfo;

    public CreatorService(String outDir, TableInfo tableInfo) {
        this.outDir = outDir + "/";
        this.tableInfo = tableInfo;
    }

    public void createEntity(){
        String demoPath = PathUtil.getAbsolutePath(PATH.DEMO_ENTITY_FILE_PATH);

        String targetPath = outDir + PATH.OUTPUT_ENTITY_PATH;
        String targetFileName = tableInfo.getClazzName();
        String targetFilePath = targetPath + targetFileName + ".java";

        String content = FileUtil.readContentFromFile(demoPath);
        content = content.replaceAll(FLAG.UCLASSNAME, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        content = content.replaceAll(FLAG.CONTENT, createEntityStr());

        FileUtil.createDirIfNotExists(targetPath);

        FileUtil.writeContentToFile(targetFilePath, content);
    }

    protected String createEntityStr(){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(createDefinition());
        stringBuffer.append(createDefaultConstructor());
        stringBuffer.append(createConstructor());
        stringBuffer.append(createGetterAndSetter());
        return stringBuffer.toString();
    }

    protected String createGetterAndSetter() {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String> mapper = new HashMap<String, String>();
        for(ColumnInfo columnInfo : tableInfo.getColumns()){
            mapper.clear();
            mapper.put(CODEDEMO.F_T, columnInfo.getFieldType());
            String methodName = columnInfo.getFieldName();
            methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1,
                    methodName.length());
            mapper.put(CODEDEMO.F_P, methodName);
            mapper.put(CODEDEMO.F_p, columnInfo.getFieldName());
            stringBuffer.append(handleMyReplace(CODEDEMO.GETTER_DMEO, mapper));
            stringBuffer.append(handleMyReplace(CODEDEMO.SETTER_DEMO, mapper));
        }
        return stringBuffer.toString();
    }

    protected String createDefaultConstructor() {
        Map<String, String> mapper = new HashMap<String, String>();
        mapper.put(CODEDEMO.F_C, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        mapper.put(CODEDEMO.F_PL, "");
        mapper.put(CODEDEMO.F_CS, "");
        return handleMyReplace(CODEDEMO.CONSTRUCTOR_DEMO, mapper);
    }

    protected String createConstructor() {
        StringBuffer pl = new StringBuffer();
        StringBuffer cs = new StringBuffer();
        Map<String, String> mapper = new HashMap<String, String>();
        for(ColumnInfo columnInfo : tableInfo.getColumns()){
            pl.append(columnInfo.getFieldType() + CODEDEMO.B + columnInfo.getFieldName() + "," +
                    CODEDEMO.B);

            mapper.put(CODEDEMO.F_p, columnInfo.getFieldName());
            cs.append(handleMyReplace(CODEDEMO.CONSTRUCTOR_STATEMENT_DEMO, mapper));
        }

        pl.delete(pl.lastIndexOf(","), pl.length());

        mapper.clear();
        mapper.put(CODEDEMO.F_C, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        mapper.put(CODEDEMO.F_PL, pl.toString());
        mapper.put(CODEDEMO.F_CS, cs.toString());
        return handleMyReplace(CODEDEMO.CONSTRUCTOR_DEMO, mapper);
    }

    protected String createDefinition() {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String> mapper = new HashMap<String, String>();
        for(ColumnInfo columnInfo : tableInfo.getColumns()){
            mapper.clear();
            mapper.put(CODEDEMO.F_T, columnInfo.getFieldType());
            mapper.put(CODEDEMO.F_p, columnInfo.getFieldName());
            stringBuffer.append(handleMyReplace(CODEDEMO.DEFINITION_DEMO, mapper));
        }
        return stringBuffer.toString();
    }


    public void createDao(){
        String demoPath = PathUtil.getAbsolutePath(PATH.DEMO_DAO_FILE_PATH);

        String targetPath = outDir + PATH.OUTPUT_DAO_PATH;
        String targetFileName = NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()) + "Dao";
        String targetFilePath = targetPath + targetFileName + ".java";

        String content = FileUtil.readContentFromFile(demoPath);
        content = content.replaceAll(FLAG.UCLASSNAME, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        content = content.replaceAll(FLAG.FILENAME, targetFileName);

        FileUtil.createDirIfNotExists(targetPath);

        FileUtil.writeContentToFile(targetFilePath, content);
    }

    public void createServiceInterface(){
        String demoPath = PathUtil.getAbsolutePath(PATH.DEMO_SERVICE_INTERFACE_FILE_PATH);

        String targetPath = outDir + PATH.OUTPUT_SERVICE_INTERFACE_PATH;
        String targetFileName = "I" + NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()) + "Service";
        String targetFilePath = targetPath + targetFileName + ".java";

        String content = FileUtil.readContentFromFile(demoPath);
        content = content.replaceAll(FLAG.UCLASSNAME, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        content = content.replaceAll(FLAG.FILENAME, targetFileName);

        FileUtil.createDirIfNotExists(targetPath);

        FileUtil.writeContentToFile(targetFilePath, content);
    }

    public void createServiceImpl(){
        String demoPath = PathUtil.getAbsolutePath(PATH.DEMO_SERVICE_IMPL_FILE_PATH);

        String targetPath = outDir + PATH.OUTPUT_SERVICE_IMPL_PATH;
        String targetFileName = NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()) + "ServiceImpl";
        String targetFilePath = targetPath + targetFileName + ".java";

        String content = FileUtil.readContentFromFile(demoPath);
        content = content.replaceAll(FLAG.UCLASSNAME, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        content = content.replaceAll(FLAG.FILENAME, targetFileName);

        FileUtil.createDirIfNotExists(targetPath);

        FileUtil.writeContentToFile(targetFilePath, content);
    }

    public void createAction(){
        String demoPath = PathUtil.getAbsolutePath(PATH.DEMO_ACTION_FILE_PATH);

        String targetPath = outDir + PATH.OUTPUT_ACTION_PATH;
        String targetFileName = NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()) + "Action";
        String targetFilePath = targetPath + targetFileName + ".java";

        String objName = NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName());
        objName = objName.substring(0, 1).toLowerCase() + objName.substring(1,
                objName.length());

        String content = FileUtil.readContentFromFile(demoPath);
        content = content.replaceAll(FLAG.UCLASSNAME, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        content = content.replaceAll(FLAG.LCLASSNAME, objName);
        content = content.replaceAll(FLAG.FILENAME, targetFileName);

        FileUtil.createDirIfNotExists(targetPath);

        FileUtil.writeContentToFile(targetFilePath, content);
    }

    public void createMapper(){
        String demoPath = PathUtil.getAbsolutePath(PATH.DEMO_MAPPER_FILE_PATH);

        String targetPath = outDir + PATH.OUTPUT_MAPPER_PATH;
        String targetFileName = NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()) + "Mapper";
        String targetFilePath = targetPath + targetFileName + ".xml";

        String objName = NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName());
        objName = objName.substring(0, 1).toLowerCase() + objName.substring(1,
                objName.length());

        String content = FileUtil.readContentFromFile(demoPath);
        content = content.replaceAll(FLAG.UCLASSNAME, NameUtil.specialUnderlineToCapitalizeCamel(tableInfo.getClazzName()));
        content = content.replaceAll(FLAG.LCLASSNAME, objName);
        content = content.replaceAll(FLAG.CONTENT, createMapperStr());

        FileUtil.createDirIfNotExists(targetPath);

        FileUtil.writeContentToFile(targetFilePath, content);
    }

    protected String createMapperStr() {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String> mapper = new HashMap<String, String>();
        for(ColumnInfo columnInfo : tableInfo.getColumns()){
            if(columnInfo.getColumnName().equals(columnInfo.getFieldName())){
                continue;
            }
            mapper.clear();
            mapper.put(CODEDEMO.F_p, columnInfo.getFieldName());
            mapper.put(CODEDEMO.F_c, columnInfo.getColumnName());
            stringBuffer.append(handleMyReplace(CODEDEMO.MAPPER_DEMO, mapper));
        }
        return stringBuffer.toString();
    }

    protected String handleMyReplace(String source, Map<String, String> mapper){
        source = source.replaceAll(CODEDEMO.F_TAB, CODEDEMO.TAB);
        source = source.replaceAll(CODEDEMO.F_B, CODEDEMO.B);
        source = source.replaceAll(CODEDEMO.F_NL, CODEDEMO.NL);
        for(Map.Entry<String, String> entry : mapper.entrySet()){
            source = source.replaceAll(entry.getKey(), entry.getValue());
        }
        return source;
    }

    public void autoSSM() {
        createEntity();
        createDao();
        createServiceInterface();
        createServiceImpl();
        createAction();
        createMapper();
    }
}
